package com.igorkundovic.guitarstab.service_hibernate;



import java.io.Serializable;
import java.util.List;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.igorkundovic.guitarstab.Comment;
import com.igorkundovic.guitarstab.User;
import com.igorkundovic.guitarstab.beans.CommentBean;
import com.igorkundovic.guitarstab.beans.UserBean;
import com.igorkundovic.guitarstab.service.UserService;


public class UserHibernateService implements UserService {

	private final Session session;
	
	public UserHibernateService(Session session) {
		// TODO Auto-generated constructor stub
		this.session = session;
	}
	
	
	public Long save(User user) {
		// TODO Auto-generated method stub
		Long id = (Long) session.save(user);
		
		return  id;
		
	}

	public User get(Long id) {
		// TODO Auto-generated method stub
		return (User) session.get(UserBean.class, id);
	}

	public boolean changeUserInfo(User u){
		session.update(u);
		return true;
	}
	
	public boolean deleteUser(User u){
		User u2=(User) session.merge(u);
		session.delete(u2);
		return true;
	}
	
	public User retrive(User user) {
		Criteria crit = session.createCriteria(UserBean.class).add(Restrictions.eq("userName", user.getUserName())).add(Restrictions.eq("password", user.getPassword()));
		return (User) crit.uniqueResult();
		
	}
	
	public User findUser(Serializable id){
		return (User) session.load(UserBean.class, id);
	}


	

	
}
